/*
 lib.c
 processing of various data in eeprom or RAM
 - parameters name :if LIBRAM is defined, theses parameters are in RAM 
	read  through access functions lib_getchar
	write :lib_putchar
	
other variables are always in eeprom, access with ee_getchar and ee_putchar
  - control byte for the power on, index EE_POWER
see rccl.h for the #defines

# ============================================================================
# Copyright (C) 2008 2009 Robert Spilleboudt
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#============================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <avr/eeprom.h>
#include "io.h"
#include "rccl.h"
//byte access to eeprom i = offset within the whole eeprom
void ee_putchar(int i, char c){//puts char c at offset i , returns 0 if OK , other is an error code
	eeprom_busy_wait() ;
	eeprom_write_byte ((uint8_t*) i, (uint8_t) c);  //8.5 msec !
	eeprom_busy_wait() ;	
}

char ee_getchar(int i){//get and return  char c at offset i
	eeprom_busy_wait() ;
	return (char)eeprom_read_byte ((uint8_t *) i );
}
/* lib_putchar lib_getchar give access to the parameters names
    two versions:
     - in RAM (if LIBRAM is defined), which is the normal version
     - in eeprom
*/
#ifdef LIBRAM
/* names are in RAM , this is the normal situation
In case of urgent need of memory,comment the #define  LIBRAM and all the names are moved to eeprom
but the time needed to load a model increases with several seconds 
*/
extern struct rccl_lib lib; // the parameters names are in RAM
void lib_putchar(int i, char c){//puts char c at offset i 
	lib.name[i]=c;
}

char lib_getchar(int i){//get and return  char c at offset i
	return lib.name[i];
}

#endif
#ifdef LIBROM //eeprom access for the parameters names in eeprom
void lib_putchar(int i, char c){//puts char c at offset i , returns 0 if OK , other is an error code
	ee_putchar(i+ EE_PARAM_FIRST,c);
}

char lib_getchar(int i){//get and return  char c at offset i
	eeprom_busy_wait() ;
	return (char)eeprom_read_byte ((uint8_t *) i+ EE_PARAM_FIRST);
}
#endif
#ifdef LIBRAMROM //written 27/7/2011 ...
/* names are in RAM , upto MAX_PARAM_RAM and after this limit they are in EEPROM up to the total size MAX_PARAM*/
extern struct rccl_lib lib; // the parameters names are in RAM
void lib_putchar(int i, char c){//puts char c at offset i 
	if (i<MAX_PARAM_RAM)
		{
			lib.name[i]=c;
			return;
		}
	else
		ee_putchar(i+ EE_PARAM_FIRST - MAX_PARAM_RAM,c);
}

char lib_getchar(int i){//get and return  char c at offset i
	if (i<MAX_PARAM_RAM)
		return lib.name[i];
	else 
	{
		eeprom_busy_wait() ;
		return (char)eeprom_read_byte ((uint8_t *) i+ EE_PARAM_FIRST - MAX_PARAM_RAM);
	}

}

#endif